<?php
/**
 * Created by PhpStorm.
 * User: lzh 9157374
 * Date: 2018/7/1
 * Time: 下午5:40
 */
namespace mobile\controller;
use Core\common\Wechat;
class index extends _base
{

    public function index()
    {
        global $_W;
        #判断是否微信浏览器
        if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') == false) {
            message('请在微信中打开');
            exit();
        }
        session_start();
        $aParams['field'] = ['appId','appSecret'];
        $sys = M('sys')->getData($aParams);

        if(!$_SESSION['verification_openid']){
            if(!I('code')){
                (new Wechat)->authRedirect($sys['appId'],$_W['siteroot'].gMobileUrl('index/index'));
            }
            if(I('code')){
                $userInfo = (new Wechat)->oauth2UserInfo($sys['appId'],$sys['appSecret'],I('code'));
                $_SESSION['verification_openid'] = $userInfo['openid'];
            }
            $aParams1['where'] = ['openid'=>$userInfo['openid']];
            $verification = M('verification')->getData($aParams1);
            if($verification){
                $info = [
                    'nickname'=>$userInfo['nickname'],
                    'avatar'=>$userInfo['headimgurl'],
                ];
                M('verification')->edit($info,$aParams1);
            }else{
                $info = [
                    'uniacid'=>uniacid(),
                    'openid'=>$userInfo['openid'],
                    'nickname'=>$userInfo['nickname'],
                    'avatar'=>$userInfo['headimgurl'],
                    'creation_date'=>time(),
                ];
                M('verification')->add($info);
            }
        }
        $this->display('index/index');
    }

    /*
     * 核销金额
     */
    public function money(){
        $this->StoreClerk(gMobileUrl('index/money',['TokenCode'=>I('TokenCode')]));
        if(empty(I('TokenCode'))){
            message('数据错误,请重新生成');exit();
        }
        $token = $this->tokenCheck(I('TokenCode'));
        if($token['type'] != 2){
            message('二维码错误');exit();
        }
        //判断token过期
        if(time() >= $token['endtime']){
            message('二维码已过期,请重新生成');exit();
        }

        $aParams['field'] = ['user_id','nickname','avatar','money','status'];
        $aParams['where'] = ['user_id'=>$token['user_id']];
        $user = M('user')->getData($aParams);
        if(!$user){
            message('数据错误,请重新生成');exit();
        }
        if($user['status'] == 2){
            message('此用户被关闭');exit();
        }
        $this->display('write/money',['user'=>$user,'token'=>I('TokenCode')]);
    }

    /**
     * 核销金额
     */
    public function moneyAdd(){
        $clerk = $this->StoreClerk(gMobileUrl('index/moneyAdd',['TokenCode'=>I('TokenCode')]));
        if(empty(I('TokenCode'))){
            message('未找到该用户');exit();
        }
        if(empty(I('price'))){
            message('请输入核销金额');exit();
        }
        $token = $this->tokenCheck(I('TokenCode'));

        if($token['type'] != 2){
            message('二维码错误');exit();
        }
        //判断token过期
        if(time() >= $token['endtime']){
            message('二维码已过期,请重新生成');exit();
        }

        $aParams['field'] = ['money'];
        $aParams['where'] = ['user_id'=>$token['user_id']];
        $user = M('user')->getData($aParams);

        if(I('price')>$user['money']){
            message('核销金额不能大于用户余额');exit();
        }
        pdo_begin();
        $info = [
            'money -='=>I('price'),
            'consumption +='=>I('price'),
        ];
        if(!M('user')->edit($info,$aParams)){
            pdo_rollback();
            message('数据错误');exit();
        }
        $balanceInfo = [
            'uniacid'=>uniacid(),
            'user_id'=>$token['user_id'],
            'type'=>2,
            'price'=>I('price'),
            'store_id'=>$clerk['store_id'],
            'clerk_openid'=>$clerk['openid'],
            'creation_date'=>time()
        ];
        if(!M('user_balance')->add($balanceInfo)){
            pdo_rollback();
            create_info('数据错误', '', 'error');
        }
        pdo_commit();
        create_info('核销成功', gMobileUrl('index/MoneyList'), 'success');
    }

    /**
     * 核销金额记录
     */
    public function MoneyList(){
        $clerk = $this->StoreClerk(gMobileUrl('index/MoneyList'));
        //标题
        if($title = I('title')){
            $aParams['where']['b.nickname LIKE'] = "%{$title}%";
        }
        $aParams['order'] = ['a.creation_date desc'];
        $aParams['field'] = ['b.nickname','b.avatar','a.price','a.creation_date'];
        $aParams['where']['a.clerk_openid'] = $clerk['openid'];
        $aParams['where']['a.type'] =2;
        $aParams['page'] = max(1, intval(I('page')));
        $aParams['join'] = ['leftJoin'=>[
            ['yyt_lpk_user','b','a.user_id','b.user_id']
        ]];
        $aData = M('user_balance')->page($aParams);
        $this->display('list/list',['aData'=>$aData]);
    }

    /**
     * 核销金额记录
     */
    public function MoneyList2(){
        $clerk = $this->StoreClerk(gMobileUrl('index/MoneyList'));
        //标题
        if($title = I('title')){
            $aParams['where']['b.nickname LIKE'] = "%{$title}%";
        }
        $aParams['order'] = ['a.creation_date desc'];
        $aParams['field'] = ['b.nickname','b.avatar','a.price','a.creation_date'];
        $aParams['where']['a.clerk_openid'] = $clerk['openid'];
        $aParams['where']['a.type'] =2;
        $aParams['page'] = max(1, intval(I('page')));
        $aParams['join'] = ['leftJoin'=>[
            ['yyt_lpk_user','b','a.user_id','b.user_id']
        ]];
        $aData = M('order_use')->page($aParams);
        $this->display('list/list2');
    }


    /**
     * 门店核销
     */
    public function StoreClerk($url){
        global $_W;
        #判断是否微信浏览器
        if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') == false) {
            message('请在微信中打开');
            exit();
        }
        session_start();
        if(!$_SESSION['verification_openid']){
            $aParams['field'] = ['appId','appSecret'];
            $sys = M('sys')->getData($aParams);
            if(!I('code')){
                (new Wechat)->authRedirect($sys['appId'],$_W['siteroot'].$url);
            }
            if(I('code')){
                $userInfo = (new Wechat)->oauth2UserInfo($sys['appId'],$sys['appSecret'],I('code'));
                $_SESSION['verification_openid'] = $userInfo['openid'];
            }
            $aParams1['field'] = ['store_id','openid'];
            $aParams1['where'] = ['openid'=>$userInfo['openid'],'status'=>1];
            $clerk = M('store_clerk')->getData($aParams1);
            if(!$clerk){
                message('请使用门店核销员扫码');exit();
            }
            return $clerk;
        }else{
            $aParams1['field'] = ['store_id','openid'];
            $aParams1['where'] = ['openid'=>$_SESSION['verification_openid'],'status'=>1];
            $clerk = M('store_clerk')->getData($aParams1);
            if(!$clerk){
                message('请使用门店核销员扫码');exit();
            }
            return $clerk;
        }
    }

}